package post_to_in_pkg.main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.FileChooser.ExtensionFilter;
import post_to_in_pkg.lexer.Lexer;
import post_to_in_pkg.parser.Parser;
import post_to_in_pkg.recoder.Recoder_v1;

class BtnPressedHandler implements EventHandler<ActionEvent>{
	
	FileChooser chooser=new FileChooser();
	
	public BtnPressedHandler() {
		//set file type filter
    	chooser.getExtensionFilters().add(new ExtensionFilter("TextType","*.txt"));
    	chooser.setSelectedExtensionFilter(chooser.getExtensionFilters().get(
    			chooser.getExtensionFilters().size()-1)
    			);

	}
	@Override
	public void handle(ActionEvent e){
		
		try {
		if(e.getSource().equals(Main.export_Btn)) {
			handleExport();
		}
		else if(e.getSource().equals(Main.import_Btn)) {
			handleImport();
		}
		else if(e.getSource().equals(Main.commitBtn)) {
			try {
				handleCommit();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		else {
			System.out.println("EventHandler Error");
		}
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
    private void handleCommit() throws Exception{
    	 
    	 if(Main.inputDir_tf.getText().isEmpty()||Main.outputDir_tf.getText().isEmpty()) {
    		 
    		 Main.view_tf.setText("No file is specified,use Console for interact");
    		 
    		 //how to optimize the object creation, Factory Mode? 
    		 Main.lex=new Lexer(null);
    		 Main.parser=new Parser(Main.lex);
    		 Main.parser.makeSyntaxTree();
    		 System.out.print(Main.parser.getSyntaxTree().toString());
    		 Main.parser.resetParser();
    	 }
    	 else {
    		 File ref=new File(Main.inputDir_tf.getText());
    		 FileWriter writerfile=new FileWriter(Main.outputDir_tf.getText());
    		 if(!ref.exists()&&!ref.canRead()) {
    			 writerfile.close();
    			 throw new IOException("File not exist");
    		 }
    		 Main.lex=new Lexer(ref);
    		 
    		 Main.parser=new Parser(Main.lex);
    		 Main.parser.makeSyntaxTree();
    		 Main.Recoder.setParser(Main.parser);
    		 Main.view_tf.setText(Main.view_tf.getText().concat("\nOutput:\n "+
    		 Main.Recoder.writeInBuffer().toString()));
    		 writerfile.write(Main.Recoder.writeInBuffer().toString());
    		 Main.parser.resetParser();
    		 writerfile.close();
    	 }
    	 
     }
    private void handleImport() {
    	
    	//set title 
    	chooser.setTitle("select a file to import");
    	//set the initial directory
    	chooser.setInitialDirectory(new File(System.getProperty("user.home")));
    	
    	//display select stage, get the selected file
    	File file=chooser.showOpenDialog(new Stage());
    	if(file!=null) {
    		String str=file.getAbsolutePath();
    		Main.inputDir_tf.setText(str); 
    		Main.outputDir_tf.setText(str.substring(0,str.length()-4)+"_out.txt");
    		//display content in viewTextField
    		
    		try {
				Scanner read=new Scanner(file);
				StringBuffer buf=new StringBuffer();
				while(read.hasNextLine()) {
					buf.append(read.nextLine()); buf.append('\n');
				}
				Main.view_tf.setText("Input :\n"+buf.toString());
				read.close();
			} catch (FileNotFoundException e) {
				
				e.printStackTrace();
			}
    		
    	}
    	
    }
    private void handleExport() throws IOException {
    	chooser.setTitle("select where to export");
    	chooser.setInitialFileName("Script-exported.txt");
    	File file=chooser.showSaveDialog(new Stage());
    	if(file!=null) {
    		Main.outputDir_tf.setText(file.getAbsolutePath());
    	}
    	
    }
     
}